package com.hongyun.tms.web.controller;


import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.RepeatSubmit;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.mcustomer.MCustomerImportDto;
import com.hongyun.tms.common.dto.mcustomer.MCustomerQueryDto;
import com.hongyun.tms.common.dto.mcustomer.MCustomerSaveReqDto;
import com.hongyun.tms.common.dto.mcustomer.MCustomerTemplateDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.MCustomerVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.entity.MCustomer;
import com.hongyun.tms.infra.excel.EasyExcelUtil;
import com.hongyun.tms.infra.excel.listener.MCustomerExcelListener;
import com.hongyun.tms.service.IMCustomerService;
import com.hongyun.tms.web.utils.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 客户信息表 前端控制器
 * </p>
 *
 * @author liPeng
 * @since 2024-06-06
 */
@Api(tags = "客户信息管理")
@RestController
@RequestMapping("/mCustomer")
public class MCustomerController {

    @Resource
    private IMCustomerService customerService;


    @GetMapping("list")
    public AjaxResult<PageResponseDto<MCustomerVO>> list(MCustomerQueryDto query) {
        PageResponseDto<MCustomerVO> pageList = customerService.pageList(query);
        return AjaxResult.success(pageList);
    }

    @GetMapping("getById")
    public AjaxResult<MCustomerVO> getById(@RequestParam("id") Long id) {
        MCustomer customer = customerService.getById(id);
        return AjaxResult.success(MapperingUtils.copyProperties(customer, MCustomerVO.class));
    }

    /**
     * 批量删除
     */
    @PostMapping("removeBatch")
    @ApiOperation(value = "批量删除客户")
    @RepeatSubmit
    public AjaxResult removeBatch(@RequestParam("ids") String idList) {
        List<String> ids = Arrays.asList(idList.split(","));
        customerService.deleteBatch(ids);
        return AjaxResult.success();
    }

    /**
     * 新增/修改
     */
    @PostMapping("save")
    @ApiOperation(value = "新增/修改客户信息")
    @RepeatSubmit
    public AjaxResult save(@RequestBody @Valid MCustomerSaveReqDto dto) {
        customerService.saveOrUpdateCustomer(dto);
        return AjaxResult.success();
    }

    /**
     * 导入excel
     *
     * @param file excel
     * @return result
     */
    @PostMapping("/importCustomer")
    public AjaxResult<Void> importCustomer(MultipartFile file) throws IOException {
        AjaxResult<Void> validationResult = FileUploadUtil.validateExcelFile(file);
        // 根据code判断校验是否通过
        if (validationResult.getCode() != HttpStatus.OK.value()) {
            return validationResult;
        }

        // 创建 Excel 数据处理监听器
        MCustomerExcelListener listener = new MCustomerExcelListener(customerService);

        // 处理 Excel 文件
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcelUtil.syncReadModel(inputStream, listener, MCustomerImportDto.class, 0, 1);
            return AjaxResult.success();
        }
    }

    @GetMapping("exportTemplate")
    @ExcelExport(value = MCustomerTemplateDto.class)
    public AjaxResult exportTemplate(HttpServletResponse response) {
        return AjaxResult.success(Collections.emptyList());
    }

    @GetMapping("exportData")
    @ExcelExport(value = MCustomerVO.class)
    public AjaxResult<List<MCustomerVO>> exportData(MCustomerQueryDto query, HttpServletResponse response) {
        List<MCustomerVO> exportList = customerService.export(query);
        return AjaxResult.success(exportList);
    }
}
